package org.tbjd.client.customWidgets.taskList;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.guis.taskList.items.TreeGridItem;
import org.tbjd.client.interfaces.Decisionmaker;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class TreeGridItemHandle extends FocusPanel implements Decisionmaker{
	
	private HorizontalPanel container = null;
	private FocusPanel handlePanel = null;
	private TextBox textBox = new TextBox();
	private long id = new Date().getTime();
	private TreeGridItem treeGridItem = null;
	private Task task = null;
	private Widget handleIcon = null;
	
	public TreeGridItemHandle(){}
	
	public TreeGridItemHandle(String value){
		
		container = new HorizontalPanel();
		handlePanel = new FocusPanel();
		handleIcon = new HTML("<li>&nbsp;</li>");
		
		handlePanel.add(handleIcon);
		
		handlePanel.addClickListener(new ClickListener(){

			public void onClick(Widget sender) {
				Map<Integer, Object> map = new HashMap<Integer, Object>();
				map.put(NotificationsConstants.TREEGRIDITEM_SELECTED, getTask());
				
				try {
					delegate(map);
				} catch (TBJDException e) {
					e.printStackTrace();
				}
			}

		});
		
		container.add(handlePanel);
		
		textBox.setText(value);
		textBox.setStyleName("customTextBox");
		container.add(textBox);
		
		container.setBorderWidth(0);
		
		this.add(container);
		
	}
	
	public FocusPanel getHandle(){
		return handlePanel;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	public String toString() {
		return "TreeGridItemHandle.toString() - id = " + getId();
	}

	public TreeGridItem getTreeGridItem() {
		return treeGridItem;
	}

	public void setTreeGridItem(TreeGridItem treeGridItem) {
		this.treeGridItem = treeGridItem;
	}

	public void decide(Object action) throws TBJDException {
		
	}

	public void delegate(Object action) throws TBJDException {
		System.out.println("TreeGridItemHandle.delegate()");

		// passes info to the first responsible
		Widget parent = ((Widget) this).getParent();
		while (parent != null && parent instanceof Decisionmaker == false) {
			parent = ((Widget) parent).getParent();
		}
		if (parent != null) {
			try {
				((Decisionmaker) parent).decide(action);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		} else {
			throw new NoUpperDecisionmakerFoundException();
		}
	}

	public void handle(Object action) throws TBJDException {
		
	}
	
	public void setStyle(String style){
		this.handleIcon.setStyleName(style);
	}

	public Widget getHandleIcon() {
		return handleIcon;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}
	
}
